package networking;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;

import javax.swing.JOptionPane;

public class ClientConnect 
{
	private static Socket socket;
	private static ObjectOutputStream out;
	private static ObjectInputStream in;
	
	public static boolean Connect()
	{
		try 
		{
			if(socket == null)
			{
				socket = new Socket("zmtn.dyndns-web.com", 5676);
				out = new ObjectOutputStream(socket.getOutputStream());
				in = new ObjectInputStream(socket.getInputStream());
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public static void sendMap(SMap map)
	{
		try 
		{
			out.writeObject(new Request("addMap", "", map));
			Response r = (Response) in.readObject();
			JOptionPane.showMessageDialog(null, r.getMessage());
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		
	}
	
	public static ArrayList<SMap> getCustomMaps()
	{
		ArrayList<SMap> list = null;
		try 
		{
			out.writeObject(new Request("getCustomMaps", "", null));
			Response r = (Response) in.readObject();
			list = r.getMaps();
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return list;
	}
	
	public static void Disconnect()
	{
		try 
		{
			out.writeObject(new Request("disconnect", "", null));
			socket.close();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
}
